package com.qbw.app.module.common.fragment;

import android.app.Activity;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.View;

import com.qbw.app.R;
import com.qbw.app.databinding.CommonFragmentPhotoChooseBinding;
import com.qbw.core.base.BaseBottomDialogFragment;

/**
 * @author QBW
 * @date 2019/10/18
 */
public class PhotoChooseDialogFragment extends BaseBottomDialogFragment<CommonFragmentPhotoChooseBinding> implements View.OnClickListener {

    public static PhotoChooseDialogFragment createInstance() {
        PhotoChooseDialogFragment ins = new PhotoChooseDialogFragment();
        return ins;
    }

    @Override
    protected int onGetLayoutId() {
        return R.layout.common_fragment_photo_choose;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mBinding.tvCamera.setOnClickListener(this);
        mBinding.tvGallery.setOnClickListener(this);
        mBinding.tvCancel.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int vid = v.getId();
        if (vid == R.id.tv_cancel) {
            Listener listener = getListener();
            if (listener != null) {
                listener.onClickPhotoChooseCancel();
            }
            dismiss();
        } else if (vid == R.id.tv_camera) {
            Listener listener = getListener();
            if (listener != null) {
                listener.onClickPhotoChooseCamera();
            }
            dismiss();
        } else if (vid == R.id.tv_gallery) {
            Listener listener = getListener();
            if (listener != null) {
                listener.onClickPhotoChooseGallery();
            }
            dismiss();
        }
    }

    private Listener getListener() {
        Fragment pf = getParentFragment();
        if (pf instanceof Listener) {
            return (Listener) pf;
        }
        Activity act = getActivity();
        if (act instanceof Listener) {
            return (Listener) act;
        }
        return null;
    }

    public interface Listener {
        void onClickPhotoChooseCancel();

        void onClickPhotoChooseCamera();

        void onClickPhotoChooseGallery();
    }
}
